package cn.itsource.org.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags={"部门接口"})
@RestController     //交给spring管理,Controller+ResponseBody 123侯卿666
@RequestMapping("/dept")     //暴露一级路径
public class DepartmentController {

    //注入业务层依赖
    @Autowired
    private IDepartmentService departmentService;


    @GetMapping("/treeDept")
    public AjaxResult treeList(){
        List<Department> departmentList = departmentService.TreeDept();
        return AjaxResult.createSuccess(departmentList);
    }

    /**
     * 查询所有操作
     */
    @GetMapping
    public AjaxResult findAll(){
        List<Department> departmentList = departmentService.findAll();
        return AjaxResult.createSuccess(departmentList);
    }

    /**
     * 分页查询
     */
    @PostMapping ("/findPage")
    public PageList<Department> getAll(@RequestBody DepartmentQuery query){
        return departmentService.getAll(query);

    }

    /**
     * 查询所有的上级部门们
     */
    @PostMapping("/findParentDepts")
    private AjaxResult findParentDepts(){
        //调用业务层 alt+enter
        return AjaxResult.createSuccess(departmentService.findParentDepts());
    }



    /**
     * 根据id查询
     */
    @GetMapping("/findOne/{id}")
    public Department findOne(@PathVariable("id") Long id){
        return departmentService.findOne(id);
    }


    /**
     * 新增/修改操作
     */
    @PostMapping
    @ApiOperation(value = "新增/修改部门",notes = "王权富贵")
    public AjaxResult saveOrupdate(@RequestBody Department department){

            departmentService.saveOrUpdate(department);
            return AjaxResult.createSuccess();
    }


    /**
     * 删除方法
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

            departmentService.delete(id);
            return AjaxResult.createSuccess();

    }


    /**
     *批量删除
     */
    @PostMapping("/batchDelete")
    public AjaxResult deleteBath(@RequestBody DepartmentQuery query){
            departmentService.bathDeleteById(query.getIds());
            return AjaxResult.createSuccess();

    }

}
